/**
 * Copyright(c) 2006-2008, FeyaSoft Inc.
 */
package("feyaSoft.feedback");

/**
 * This JS is mainly used to list feedback actions
 * It is under sub-menu tutorial.
 *
 * @author fzhuang
 * @Date Oct 6, 2007
 */
feyaSoft.feedback.Comments = function(feedbackDS, parentId) {

    Ext.QuickTips.init();
    
    // row expander
    var expander = new Ext.grid.RowExpander({
        tpl : new Ext.Template(
            '<hr noshade="noshade" size="1"><font size="2"><p>{description}</p></font>'
        )
    });
    
   /************************************************************
	* Display the result in page
    * the column model has information about grid columns
    * dataIndex maps the column to the specific data field in
    * the data store (created below)
    ************************************************************/
    var commentCM = new Ext.grid.ColumnModel([
        expander,
        {
           id: 'id',
           header: "Identify",
           dataIndex: 'id',
           width: 150,
           hidden: true
        },{
            header: "Subject", 
            width: 300, 
            dataIndex: 'subject'
        },{
            header: "Author", 
            width: 150, 
            dataIndex: 'author'
        },{
            header: "Update Time", 
            width: 150, 
            dataIndex: 'updateDate'
        }
    ]);   
	    
   /************************************************************
    * connect to backend - grid - core part
    * create the Data Store
    *   connect with backend and list the result in page
    *   through JSON format
    ************************************************************/    
    var dataStore = new Ext.data.Store({
        // load using script tags for cross domain, if the data in on the same domain as
        // this page, an HttpProxy would be better
        proxy: new Ext.data.HttpProxy({
           url: 'feedback/listFeedback.htm?action=listResult&parentId='+parentId
        }),
        
        // create reader that reads the Topic records
        reader: new Ext.data.JsonReader({
            root: 'results',
            totalProperty: 'totalCount',
            id: 'id'
        }, [
            {name: 'id'},
            {name: 'subject'},
            {name: 'author'},
            {name: 'updateDate'},
            {name: 'description'}
        ])
    });
	// trigger the data store load
	dataStore.load();	
	
   /************************************************************
    * Define menubar now in here
    *   add and search functions
    ************************************************************/     
    var menubar = [{
        text:'New Comment',
        tooltip:'Start post a new comment',
        iconCls:'addItem',
        handler: function(){
            // add new comment now - reuse createFeedback
            feyaSoft.feedback.CreateFeedback(dataStore, parentId, feedbackDS);
        }
    }];
	
   /************************************************************
    * add a paging toolbar to the grid's footer 
    ************************************************************/  
    var myPagingToolbar = new Ext.PagingToolbar({
        pageSize: 10,
        displayInfo: true,
        displayMsg: 'total {2} results found. Current shows {0} - {1}',
        emptyMsg: "not result to display",
        store: dataStore
    });

   /************************************************************
    * Define the grid panel now
    ************************************************************/     
    var commentGrid = new Ext.grid.GridPanel({
        border: false,
        ds: dataStore,
        cm: commentCM,
        viewConfig: {forceFit:true},
        tbar: menubar,
        bbar: myPagingToolbar,
        plugins: expander	 
    });

   /************************************************************
    * create a new window now
    ************************************************************/     
    if (!win) {
        var win = new Ext.Window({
            title:'List comments for this feedback',
            width:695,
            height:430,
            x:180,
            y:100,
            iconCls: 'comments',
            shim:false,
            animCollapse:false,
            constrainHeader:true,

            layout: 'fit',
            items: commentGrid
        });	            
    }
    win.show(); 
    
   /************************************************************
    * Action - edit
    *   handle double click 
    *   user select one of the item and want to update it
    ************************************************************/
    commentGrid.on('rowdblclick', function(commentGrid, rowIndex, e) {
	    var parentId = dataStore.data.items[rowIndex].id;  
	    //new feyaSoft.feedback.EditFeedback(dataStore, parentId);  
	}); 	    

};